package com.example.myroom;

import androidx.appcompat.app.AppCompatActivity;
import androidx.room.Room;
import androidx.room.RoomDatabase;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

public class MainActivity extends AppCompatActivity {
private WordDao wordDao;
private WordDatabase wordDatabase;
private Button zeng,shan,gai,cha;
private TextView txt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        zeng = findViewById(R.id.zeng);
        shan = findViewById(R.id.shan);
        gai = findViewById(R.id.gai);
        cha = findViewById(R.id.cha);
        txt = findViewById(R.id.txt);
        wordDatabase = Room.databaseBuilder(this,WordDatabase.class,"Word")
                .allowMainThreadQueries()
                .build();
        wordDao = wordDatabase.wordDao();
        zeng.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Word word = new Word("hellow","nihiao");
                Word word1 = new Word("hellow","nihiao");
                wordDao.insertWord(word,word1);
                updataview();
            }
        });
        shan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                wordDao.deleteAllWord();
                updataview();
            }
        });
        gai.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Word word = new Word("你好","你好");
                word.setId(1);
                wordDao.updateWord(word);
                updataview();
            }
        });
        cha.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<Word> list = wordDao.selectWord();
                Toast.makeText(MainActivity.this, list.toArray().toString(), Toast.LENGTH_SHORT).show();
            }
        });
    }
    void updataview(){
        List<Word>list = wordDao.selectWord();
        String text = "";
        for (int i = 0; i < list.size(); i++) {
            Word word = list.get(i);
            text+= word.getId()+":"+word.getWord()+"="+word.getChineseMeaning();
        }
        txt.setText(text);
    }
}